package com.jishuo.nontax.bfc.tlq;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.*;

/**
 * JMS消息接收
 */
@Slf4j
@Data
@Service
public class JmsReceive implements SessionAwareMessageListener {

    @Resource
    private MessageService messageService;

    private String queue;

    /**
     * 消息接收侦听函数
     * @param message
     * @param session
     * @throws JMSException
     */
    public void onMessage(Message message, Session session) throws JMSException {
        String msgContent ="";
        try{
            if(message instanceof BytesMessage){
                msgContent = ( (BytesMessage)message).readUTF();
            }else{
                //此处直接对接收到的
                msgContent = ( (TextMessage)message).getText();
            }
        }catch(Exception e){
            log.info("解析Jms接收消息失败"+e.toString());
        }
        try{
            //保存成文件
            //String filepath = FileUtil.createLogFile(msgContent,"0");
            //分析处理信息
            if(msgContent!=null && !msgContent.equals("")){
                messageService.receiveMessage(queue, message.getJMSMessageID(),msgContent,"");
            }
            //filepath=null;
        }catch(Exception e){
            log.info("处理Jms接收消息失败"+e.toString());
        }
    }
}
